package com.kanshu.ksgb.fastread.doudou.module.bookcity.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dl7.recycler.adapter.BaseQuickAdapter;
import com.dl7.recycler.adapter.BaseViewHolder;
import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.app.constants.Constants;
import com.kanshu.ksgb.fastread.doudou.common.imageloader.GlideBitmapLoadingListener;
import com.kanshu.ksgb.fastread.doudou.common.imageloader.GlideImageConfig;
import com.kanshu.ksgb.fastread.doudou.common.imageloader.GlideImageLoader;
import com.kanshu.ksgb.fastread.doudou.common.util.DisplayUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.Utils;
import com.kanshu.ksgb.fastread.doudou.common.business.commonbean.BookInfo;
import com.kanshu.ksgb.fastread.doudou.common.business.utils.BookUtils;
import com.makeramen.roundedimageview.RoundedImageView;

import java.util.List;

/**
 * 排行榜适配器
 * 收藏
 * Created by wcy on 18/5/24.
 */

public class RankingCollectAdapter extends BaseQuickAdapter<BookInfo> {
    GlideImageConfig mConfig;
    public RankingCollectAdapter(Context context, List<BookInfo> data) {
        super(context, data);
        mConfig = new GlideImageConfig.Builder().
                setCropType(GlideImageConfig.CENTER_CROP).
                setAsBitmap(true).
                setPlaceHolderResId(R.color.gray).
                setErrorResId(R.color.gray).
                setAnimResId(R.anim.fade_out).
                setDiskCacheStrategy(GlideImageConfig.DiskCache.ALL).
                setPriority(GlideImageConfig.LoadPriority.NORMAL).build();
    }

    private void setDefaultHeader(ImageView imageView, int site) {
        if (site == Constants.UNKNOWN) {
            imageView.setImageResource(R.mipmap.ic_collect_default);
        } else if (site == Constants.MALE) {
            imageView.setImageResource(R.mipmap.ic_collect_male);
        } else if (site == Constants.FEMALE) {
            imageView.setImageResource(R.mipmap.ic_collect_female);
        }

    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.item_ranking_collect_layout;
    }

    @Override
    protected void convert(BaseViewHolder holder, BookInfo item, int position) {
        position = position - getHeaderViewsCount();
        LinearLayout container = holder.getView(R.id.collect_header_container);
        RelativeLayout collectContainer = holder.getView(R.id.collect_container);
        if (position < 3) {
            holder.setTextColor(R.id.ranking, Color.parseColor("#ff5a4d"));
            DisplayUtils.visible(container, collectContainer);
            for (int i = 0; i < container.getChildCount(); i++) {
                container.getChildAt(i).setVisibility(View.GONE);
            }
            if (Utils.isEmptyList(item.collect_user)) {
                DisplayUtils.gone(container, collectContainer);
            } else {
                DisplayUtils.visible(container, collectContainer);
                for (int i = 0; i < item.collect_user.size(); i++) {
                    LinearLayout linearLayout = (LinearLayout) container.getChildAt(i);
                    linearLayout.setVisibility(View.VISIBLE);
                    TextView tv = (TextView) linearLayout.getChildAt(1);
                    FrameLayout frameLayout = (FrameLayout) linearLayout.getChildAt(0);
                    final RoundedImageView imageView  = (RoundedImageView) frameLayout.getChildAt(0);
                    tv.setText(item.collect_user.get(i).nickname);
                    final int pos = i;
                    setDefaultHeader(imageView, item.collect_user.get(i).sex);
                    GlideImageLoader.load(item.collect_user.get(i).headimgurl, imageView, mConfig, new GlideBitmapLoadingListener() {
                        @Override
                        public void onSuccess(Bitmap bitmap) {
                            imageView.setImageBitmap(bitmap);
                        }
                        @Override
                        public void onError() {
                            setDefaultHeader(imageView, item.collect_user.get(pos).sex);
                        }
                    });
                }
            }
        } else {
            DisplayUtils.gone(container, collectContainer);
            holder.setTextColor(R.id.ranking, Color.parseColor("#ffa889"));
        }
        holder.setText(R.id.ranking, BookUtils.getRankingNum(position));
        holder.setText(R.id.book_title, item.book_title)
                .setText(R.id.people_num, item.week_collect_num + "人收藏");
        GlideImageLoader.load(item.cover_url, holder.getView(R.id.cover));
        holder.setText(R.id.author, item.author_name)
                .setText(R.id.label, BookUtils.getBookLabel(item))
                .setText(R.id.book_status, BookUtils.getBookStatus(item));


    }
}
